package com.avic.rong.wedgit.customView;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;

import com.avic.rong.R;
import com.avic.rong.utils.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Chenjiaxin on 2019/7/15 20:22
 * E-Mail Address：16601203965@163.com
 */
public class BookPublishItemAdapter extends RecyclerView.Adapter<BookPublishItemAdapter.BookPublishViewHolder> {


    private Context context;
    private List<ItemSearchBean.SubBean> dataList = new ArrayList<>();
    private SearchItemView searchItemView;
    private TextView popWindowTextView;

    public void setPopWindowTextView(TextView popWindowTextView) {
        this.popWindowTextView = popWindowTextView;
    }

    private boolean isMultiSelected = true;


    public void setMultiSelected(boolean multiSelected) {
        isMultiSelected = multiSelected;
    }

    public BookPublishItemAdapter(Context context, SearchItemView searchView) {
        this.context = context;
        this.searchItemView = searchView;
    }

    public void setDataList(List<ItemSearchBean.SubBean> dataList) {
        this.dataList = dataList;
    }


    public BookPublishItemAdapter(Context context) {
        this.context = context;
    }


    @NonNull
    @Override
    public BookPublishViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_book_publish, viewGroup, false);
        return new BookPublishViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final BookPublishViewHolder holder, final int position) {

        ItemSearchBean.SubBean searchBean = dataList.get(position);

        holder.tv_name.setText(searchBean.getName());
        holder.tv_name.setSelected(searchBean.isChecked());
        holder.checkbox.setChecked(searchBean.isChecked());
        holder.itemView.setOnClickListener(new BookPublishClickListener(searchBean, position));


    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }



    class BookPublishClickListener implements View.OnClickListener {

        ItemSearchBean.SubBean searchBean;
        int position;

        public BookPublishClickListener(ItemSearchBean.SubBean searchBean, int position) {
            this.searchBean = searchBean;
            this.position = position;
        }

        @Override
        public void onClick(View v) {
            if (!isMultiSelected) {
                for (int index = 0; index < dataList.size(); index++) {
                    if (index != position) {
                        dataList.get(index).setChecked(false);
                    }
                }
            }
            searchBean.setChecked(!searchBean.isChecked());
            notifyDataSetChanged();
            String selectStr = "";
            for (ItemSearchBean.SubBean searchBean : dataList) {
                if (searchBean.isChecked()) {
                    selectStr = selectStr + searchBean.getName() + ",";
                }
            }
            if (!StringUtils.isEmpty(selectStr)) {
                selectStr = selectStr.substring(0, selectStr.length() - 1);
            } else {
                selectStr = "全部";
            }
//            searchItemView.tv_show_select.setText(selectStr);
            popWindowTextView.setText(selectStr);

        }
    }




    class BookPublishViewHolder extends RecyclerView.ViewHolder {
        TextView tv_name;
        CheckBox checkbox;

        public BookPublishViewHolder(@NonNull View itemView) {
            super(itemView);
            tv_name = itemView.findViewById(R.id.tv_name);
            checkbox = itemView.findViewById(R.id.checkbox);
        }
    }




}
